package com.example.mybatis_demo.mapper;

import com.example.mybatis_demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    //基于XML格式执行SQL语句


    List<UserInfo> selectAll();

    Integer insertUser(UserInfo userInfo);

    void deleteUser(UserInfo userInfo);

    Integer updateUser(UserInfo userInfo);

    Integer insertUser2(@Param(("u")) UserInfo userInfo);

    Integer insertUser3(UserInfo userInfo);

    Integer updateInfo(UserInfo userInfo);

    Integer insertUser4(UserInfo userInfo);

    //使用trim标签处理and关键字
    List<UserInfo> selectByCondition(UserInfo userInfo);
    //使用where标签处理and关键字
    List<UserInfo> selectByCondition2(UserInfo userInfo);

    @Select("<script>" +
            "select * from userinfo " +
            "<where>" +
            "<if test=\"userName != null\">" +
            "username = #{userName}" +
            "</if>" +
            "<if test=\"age != null\">" +
            "and age = #{age}" +
            "</if>" +
            "<if test=\"gender != null\">" +
            "and gender = #{gender}" +
            "</if>" +
            "<if test=\"phone != null\">" +
            "and phone = #{phone}" +
            "</if>" +
            "</where>" +
            "</script>")
    @Results(id = "BaseMap",value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<UserInfo> selectByCondition3(UserInfo userInfo);


    @Select("<script>" +
            "select * from userinfo " +
            "<where>" +
            "<if test=\"userName != null\">" +
            "username = #{userName}" +
            "</if>" +
            "<if test=\"age != null\">" +
            "and age = #{age}" +
            "</if>" +
            "<if test=\"gender != null\">" +
            "and gender = #{gender}" +
            "</if>" +
            "<if test=\"phone != null\">" +
            "and phone = #{phone}" +
            "</if>" +
            "</where>" +
            "</script>")
    @ResultMap(value = "BaseMap")
    List<UserInfo> selectByCondition4(UserInfo userInfo);


    Integer updateInfo2(UserInfo userInfo);

    Integer deleteById(@Param("ids")List<Integer> ids);

}
